<?php
    //error_reporting(1) creates error_log file and writes errors
	error_reporting(1);

	include "config.php";

	$header = apache_request_headers();	
    $command = null;
    $data = null;

    // Convert the headers array to a readable string format for writing to a file
    $headerString = print_r($header, true);
	// Specify the file path where you want to save the headers
    $filePath = 'debug_file.txt';
    // Write the headers to the file
    file_put_contents($filePath, $headerString, FILE_APPEND);
    

	if (!$header["dev_id"]) 
	    die("Not a Silicon Wireless Systems Face Device, No dev_id found in header");

	$trans_data = new silicon_data;	
    $trans_data->appendfile = $header["dev_id"]."_tmp.dat";//".$header["trans_id"]."
	
	if (!$header["blk_no"]) 
	    $header["blk_no"] = 0;

    if ($header["blk_no"] == 0) //if the data in request from device not needed to append
    {
        //get the attendance data
    	$input_valus = $trans_data->get();
    
        // Write the values to the file
        //DeviceName “user_id” “verify_mode” “io_mode” “io_time” “log_image”
        file_put_contents($filePath, print_r($input_valus, true), FILE_APPEND);
     	
    	$device_id = $header["dev_id"];
    	
    	switch ($header["request_code"]) 
    	{
        	case "realtime_glog" : 
        		insert_log_data($device_id, $input_valus);//!!
        		break;
        	case "realtime_enroll_data" : 
        		break;
        	case "receive_cmd" : 
                set_device($device_id, $input_valus); // If ping received from new device, update it in DB
        		break;
        	default:
        		break;
    	}
    	
        if (file_exists($trans_data->appendfile)) 
            unlink($trans_data->appendfile);
    }
    else{ // append if the resonse for command from the device has data to be appened
        
            $trans_data->append($header["blk_no"]);
    } 
    

    //respond to the device 
	$trans_data->set($command, $data);

?>